package office.service.sale.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import office.action.sale.form.CustomerConditionForm;
import office.action.sale.form.CustomerFamilyForm;
import office.action.sale.form.CustomerFamilyInfoForm;
import office.action.sale.form.CustomerForm;
import office.action.sale.wrapper.CustomerWrapper;
import office.action.workflow.form.RemindForm;
import office.bo.sale.impl.Customer;
import office.bo.sale.impl.CustomerFamily;
import office.bo.sale.impl.CustomerFamilyInfo;
import office.bo.sale.impl.CustomerProp;
import office.domainset.BizDomainProviderSetImpl;
import office.service.sale.ICustomerFamilyService;
import office.service.workflow.IRemindService;
import platform.common.dao.IDomainProvider;
import platform.common.dao.impl.DaoConfigException;
import platform.common.dao.impl.DaoException;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;

public class CustomerFamilyServiceImpl extends BaseBizServiceImpl implements ICustomerFamilyService{
	private  IRemindService remindService;
	public void editCustomer(CustomerFamilyForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CustomerFamily bo = (CustomerFamily)domainProviderSetImpl.getCustomerFamilyDP().newEmptyObj();
			bo =(CustomerFamily) form.copyPropertiesToBO(bo);
			Customer customer = new Customer();
			customer.setId(form.getCustomerForm().getId().toString());
			bo.setCustomer(customer);
			for (Iterator<CustomerFamilyInfoForm> iterator = form.getSetInfo().iterator(); iterator.hasNext();) {
				CustomerFamilyInfoForm customerFamilyInfoForm = (CustomerFamilyInfoForm) iterator.next();
				CustomerFamilyInfo customerFamilyInfo = new CustomerFamilyInfo();
				CustomerProp customerProp = new CustomerProp();
				customerProp.setId(customerFamilyInfoForm.getCustomerPropId());
				if(customerFamilyInfoForm != null && customerFamilyInfoForm.getId() != null && customerFamilyInfoForm.getId().equals("")){
					customerFamilyInfoForm.setId(null);
				}
				customerFamilyInfo.setId(customerFamilyInfoForm.getId());
				customerFamilyInfo.setContent(customerFamilyInfoForm.getContent());
				customerFamilyInfo.setCustomerProp(customerProp);
				customerFamilyInfo.setCustomerFamily(bo);
				bo.getSetInfo().add(customerFamilyInfo);
			}
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑客户失败!", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<CustomerFamily> getCustomerFamilyBuCustomerId(String customerId)throws BusinessException{
		String params[] = {customerId};
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			IDomainProvider customerFamilyDP = domainProviderSetImpl.getCustomerFamilyDP();
			List<CustomerFamily> list = (List<CustomerFamily>)domainProviderSetImpl.getDao().findByHqlNameAndDomian(customerFamilyDP,"customerFamilyBuCustomerId",params);
			return list;
		} catch (DaoException e) {
			log.error("", e);
			return null;
		} catch (DaoConfigException e) {
			log.error("", e);
			return null;
		}
	}

	public void delCustomerFamilyById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerFamilyDP();
			Map map = new HashMap();
			map.put("customerFamilyId", id);
			domainProviderSetImpl.getDao().exeByHqlNameAndDomian(provider, "deleteCustomerFamilyInfoByCustomerFamilyId", map);
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除客户失败！", e);
		}
	}
	
	public CustomerFamilyForm getById(String id)throws BusinessException{
		CustomerFamilyForm form = new CustomerFamilyForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			CustomerFamily bo = (CustomerFamily)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getCustomerFamilyDP(), id);
			form.copyPropertiesFromBO(bo);
			return form;
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取客户失败!", e);
		}
	}
	public List<CustomerWrapper> findAllCustomer(CustomerConditionForm condition)throws BusinessException{
		List<CustomerWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getCustomerDP();
			Map condMap= new HashMap();
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findAllCustomer",condMap,new HashMap());
			CustomerWrapper wrapper;
			for(int i=0;i<list.size();i++){
				Customer bo = (Customer)list.get(i);
				wrapper = new CustomerWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取客户列表失败！", e);
		}
		return wrapperList;
		
	}
	
	public void sendNote() throws BusinessException {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			StringBuffer  buffer=new StringBuffer();
			buffer.append("select  t.* from  SALE_Customer t ,");
			buffer.append(" ( select   t.salesman  as salesman, max(t.updatedate) as updatedate from  SALE_Customer t  ");
			buffer.append("  where (sysdate-t.updatedate  )>25  ");
			buffer.append("  group by  t.salesman ) tt ");
			buffer.append("  where t.updatedate=tt.updatedate and  t.salesman=tt.salesman ");
			List list=domainProviderSetImpl.getDao().getBySql(buffer.toString(), Customer.class);
			if(!list.isEmpty()){
				for (Object item : list) {
					Customer bo=(Customer)item;
					RemindForm form=buildRemindFormByCustomer(bo);
					remindService.editRemind(form);
				}
			}
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("发送客户提醒失败!", e);
		}
	}
	
	private  RemindForm buildRemindFormByCustomer(Customer  customer) throws BusinessException{
		String msg="您这个月没有对客户进行过任何操作";
		RemindForm form=new RemindForm();
		form.setObjId(customer.getId().toString());
		form.setObjClassName("Customer");
		form.setName(msg);
		form.setSendDate(new Date());
		form.setRemindUser(customer.getSalesMan());
		form.setHandleState(0);
		return form;
	}

	public IRemindService getRemindService() {
		return remindService;
	}

	public void setRemindService(IRemindService remindService) {
		this.remindService = remindService;
	}

}
